package com.jt.tenant.widget;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.jt.tenant.R;

import java.util.List;

public abstract class CustomSpinner<T> extends LinearLayout implements
		OnClickListener {
	private View header;
	private TextView headerText;
	// private ImageView headerImage;
	private PopupWindow popupWindow;
	private LinearLayout list;

	private OnSpinnerClickListener<T> onItemClickListener;

	public CustomSpinner(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView();
		initListener();
		initPopupWindow();
	}

	private void initView() {
		LayoutInflater.from(getContext())
				.inflate(R.layout.custom_spinner, this);
		header = findViewById(R.id.widget_spinner_header);
		headerText = (TextView) findViewById(R.id.widget_spinner_header_text);
		// headerImage = (ImageView)findViewById(R.id.widget_spinner_header_image);
	}

	private void initListener() {
		setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				windowOnOff();
			}
		});
	}

	private void initPopupWindow() {
		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.custom_spinner_popup, null);
		list = (LinearLayout) view.findViewById(R.id.widget_spinner_popup_list);
		popupWindow = new PopupWindow(view, LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT);
		popupWindow.setOutsideTouchable(true);
		popupWindow.update();
		popupWindow.setTouchable(true);
		popupWindow.setFocusable(false);
	}

	public void createItem(List<T> arrayT) {
		for (T t : arrayT)
			createItem(t);
	}

	public void createItem(T t) {
		View view = LayoutInflater.from(getContext()).inflate(
				R.layout.custom_spinner_popup_item, null);
		TextView text = (TextView) view
				.findViewById(R.id.custom_spinner_popup_item_text);
		text.setText(setEnableItemName(t));
		text.setOnClickListener(this);
		text.setTag(t);
		list.addView(view);
	}

	@Override
	public void onClick(View v) {
		TextView text = (TextView) v
				.findViewById(R.id.custom_spinner_popup_item_text);
		setSelect((T) text.getTag());
		if (onItemClickListener != null)
			onItemClickListener.onItemClick((T) v.getTag());
	}

	public void setDefault(int index) {
		
		View view = list.getChildAt(index);
		TextView text = (TextView) view
				.findViewById(R.id.custom_spinner_popup_item_text);
		setHeader((T) text.getTag());
	}

	public T getCurrent() {
		return (T) headerText.getTag();
	}

	public void show() {
		// headerImage.setImageResource(R.drawable.widget_spinner_top);
		popupWindow.setWidth(header.getWidth());
		popupWindow.showAsDropDown(header);
	}

	public void dismiss() {
		// headerImage.setImageResource(R.drawable.widget_spinner_bottom);
		popupWindow.dismiss();
	}

	private void setSelect(T t) {
		setHeader(t);
		windowOnOff();
	}

	private void setHeader(T t) {
		headerText.setTag(t);
		headerText.setText(setEnableItemName(t));
	}

	private void windowOnOff() {
		if (popupWindow.isShowing()) 
		{
			dismiss();
		}
		else 
		{
			if (onItemClickListener != null)
				onItemClickListener.onShow();
			show(); 
		}
	}

	public void setOnItemClickListener(
			OnSpinnerClickListener<T> onItemClickListener) {
		this.onItemClickListener = onItemClickListener;
	}

	public interface OnSpinnerClickListener<T> {

		public void onShow();

		public void onItemClick(T t);
	}

	public abstract String setEnableItemName(T t);
	
	public void ClearSpinner(){
		list.removeAllViews();
	}
	public void setString(String str){
		headerText.setText(str);
	}
	public String getString(){
		return headerText.getText().toString().trim();
	}
}
